﻿//
// Copyright (c) 2011 HyperCrunch, Inc. http://www.hypercrunch.com 
// Author: Nik Kalyani  http://www.kalyani.com (Twitter: @techbubble)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Web;
using RazorEngine;
using System.Collections.Generic;

namespace HyperCrunch.Druid.Mobile
{
    /// <summary>
    /// Renders mobile navigation of a DotNetNuke portal using jQuery Mobile
    /// </summary>
    public class SiteRenderer : IContentRenderer
    {
        public ActionResult RenderContent(UIContext uiContext)
        {
            // Check for vocabulary
            string vocabularyName = String.Empty;
            if (uiContext.JsonSettings.ContainsKey("siteRenderer"))
            {
                Dictionary<string, object> settings = (Dictionary<string, object>)uiContext.JsonSettings["siteRenderer"];
                if (settings.ContainsKey("vocabulary"))
                    vocabularyName = settings["vocabulary"].ToString();
            }

            if (uiContext.Context.Request.QueryString[Globals.MOBILE_ROUTE_PARAMETER] != null)
            {
                SiteController controller = new SiteController();
                return controller.HandleRequest(null, uiContext);
            }
            else
            {
                string siteView = SiteController.GetView(uiContext);
                string siteScript = SiteController.GetAssetUrl(uiContext, Globals.RAZOR_MOBILE_SITE_SCRIPT);
                string siteStylesheet = SiteController.GetAssetUrl(uiContext, Globals.RAZOR_MOBILE_SITE_STYLESHEET);
                string pageHtml = String.Empty;
                SiteModel siteModel = new SiteModel
                {
                    AppContext = uiContext,
                    Content = SiteController.GetContent(uiContext, vocabularyName),
                    Script = siteScript,
                    Stylesheet = siteStylesheet
                };

                if (!String.IsNullOrEmpty(siteView))
                    pageHtml = Razor.Parse(siteView, siteModel);
                else
                    pageHtml = "Could not find the required view script in either the Host or the Site folder.";

                List<HttpCookie> cookies = new List<HttpCookie>();
                cookies.Add(new HttpCookie(HyperCrunch.Druid.Globals.SITE_RETURN_URL_COOKIE_NAME, uiContext.Context.Request.Url.AbsoluteUri));

                ContentResult contentResult = new ContentResult
                {
                    Content = pageHtml,
                    ContentType = "text/html",
                    Cookies = cookies
                };

                return contentResult;
            }
        }
    }
}
